package com.luoqiz.demo.threadDemo;

import java.util.ArrayList;
import java.util.List;

public class Pool {
	private List<String> arr = new ArrayList<String>();
	private int poolMaxSize=20;
	private int i=1;
	public void setPoolMaxSize(int poolMaxSize){
		this.poolMaxSize=poolMaxSize;
	}
	
	
	public synchronized void add(){
		while(arr.size()>=poolMaxSize){
			try {
				System.out.println(Thread.currentThread().getName()+"...wait");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		String food="食物"+(i++); 
		System.out.println(Thread.currentThread().getName()+"---生成:"+food);
		arr.add(food);
		this.notify();
	}
	public synchronized void remove(){
		while(arr.size()<1){
			try {
				this.wait();
				System.out.println(Thread.currentThread().getName()+"...wait");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		String c=arr.remove(0);
		System.out.println(Thread.currentThread().getName()+"---消费:"+c);
		this.notify();
	}
}
